define(['backbone', 'swiper', 'view/bannerView', 'view/newsView', 'text!../../tpl/face/face.html'],
    function(Backbone, Swiper, BannerView, NewsView, template){

    var mySwiper;

    var AppView = Backbone.View.extend({
        el: $("#view-container"),
        events: {
            "tap #news-box .navs .nav":  "switchArea",
            "click #news-box .navs .nav":  "switchArea"
        },

        initialize: function (options) {
            this.delegateEvents();
        },

        render: function () {
            this.$el.html(template);
            var bannerView = new BannerView({
                renderafterCallback: function () {
                    new Swiper ('#face-banner .swiper-container', {
                        autoplay: 4000,
                        direction: 'horizontal',
                        loop: true,
                        // 如果需要分页器
                        pagination: '.swiper-pagination'
                    });
                }
            });
            bannerView.render();

            new NewsView({
                renderafterCallback: function () {
                    mySwiper = new Swiper ('#news-box .swiper-container', {
                        direction: 'horizontal',
                        loop: true,
                        onSlideChangeEnd: function (swiper) {
                            var oNavs = $('#news-box .navs .nav');
                            var index = swiper.activeIndex;
                            oNavs.removeClass('active');
                            oNavs.eq(index - 1).addClass('active');
                        }
                    });
                }
            }).render();
        },

        switchArea: function (e) {
            var oNavs = $('#news-box .navs .nav');
            oNavs.removeClass('active');
            var target = $(e.currentTarget || e.toElement || e.target);
            target.addClass('active');
            mySwiper.slideTo(target.index() + 1);
            e.preventDefault();
        }
    });
    return AppView;
});
